import WxValidate from "../../../../utils/wx-validate/WxValidate";
import Promise from '../../../../utils/es6-promise/es6-promise'
import regeneratorRuntime from '../../../../utils/regenerator-runtime/runtime'
const utils = require('../../../../utils/util.js');
const app=getApp();
Page({
  data: {
    url:"/Api/Task/preTrailDetail",
    submitUrl:"/Api/Task/doPreTrail",
    info:null,
    listid:null,
    token:null,
    status:2,
    reason:"",
    placeholder:["不能办理，请说明理由","能办理，请说明情况"],
    radioArr:[
      {
        name:"是",
        value:"2",
        checked:true
      },
      {
        name:"否",
        value:"1",
        checked:false
      }
    ]
  },
  submitForm: async function(e){
    var params = e.detail.value;
    //debugger;
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0];
      this.showModal(error);
      return false;
    }
    params.token = this.data.token;
    var result=await this._submitData(params);
    if(result.state){
      var msg=result.msg;
      wx.showModal({
        title:msg,
        showCancel:false,
        complete: data=>{
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
    }
  },
  showModal(error,cb) {
    wx.showModal({
      reason: error.msg || error,
      showCancel: false,
      success(res) {
        if(cb) cb();
      }
    });
  },
   onLoad: async function (params) {
    wx.showLoading({
      title:"加载中..."
    })
     console.log("onLoad");
    var listid=params.listid;
    var reason=params.reason;
    var status=params.status;
    this.setData({
      listid:listid,
      reason:reason,
      status:status
    })
    const token = await app.getToken();
    this.setData({
      token:token
    })
    var result=await this._loadData();
    wx.hideLoading();
    if(result.state){
      var info=result.info;
      this.setData({
        info:info
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
    }
    this.initValidate();
  },
  initValidate() {
    // 验证字段的规则
    const rules = {
      reason: {
        required: true
      }
    };

    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = {
      reason: {
        required: "请填写内容"
      }
    };
    // 创建实例对象
    this.WxValidate = new WxValidate(rules, messages);
  },
  _loadData:function(){
    var p = new Promise((resolve,reject)=>{
        //做一些异步操作
        app.request(this.data.url,{token:this.data.token,listid:this.data.listid},res=>{
          //console.log(res);
          console.log("res");
          console.log(res);
          var data=res.data;
          // var data={
          //   state:true,
          //   data:
          //   {
          //     "headimg":"/images/card-on.png",
          //     "nickname":"撒大大的独家账号",
          //     "username":"张珊珊",
          //     "userphone":"13765843421",
          //     "remark":"遇到什么什么情况，将要怎样怎样；",
          //     "status":"1"
          //   }
          // };
          //debugger;
          if(data.state){
            //resolve({state:true,info:JSON.parse(data.info)});
            resolve({state:true,info:data.data});
          }
          else{
            resolve({state:false,msg:data.msg});          
          }
        },error=>{
          resolve({state:false,msg:"刷新失败，请重试"});
        })
    });
    return p;
  },
  _submitData:function(params){
    var p = new Promise((resolve,reject)=>{
        //做一些异步操作
        app.request(this.data.submitUrl,params,res=>{
          var data=res.data;
          if(data.state){
            resolve({state:true,msg:data.msg});
          }
          else{
            resolve({state:false,msg:data.msg});          
          }
        },error=>{
          resolve({state:false,msg:"提交失败，请重试"});
        })
    });
    return p;
  },
  radioChange:function(e){
    console.log(e);
    var value=e.detail.value;
    this.setData({
      state:value
    })
  },
  showImage:function(e){
    var url=e.currentTarget.dataset.url;
    var urls=e.currentTarget.dataset.urls;
    wx.previewImage({
      current: url,
      urls: urls
    })
  }
})